<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>表单模型配置</title>
<{$showjs}>
<link rel="stylesheet" type="text/css" href="/Plug-in/ext/resources/css/xtheme-slate.css" />
<{$ajax}>
<link rel="stylesheet" type="text/css" href="../css/main1.css" />
<style type=text/css>
<!--
.x-btn-left{
	background-position:2px 0;
}
.x-btn-over .x-btn-left{
	background-position:2px -63px;
}
--> </style>
<script type="text/javascript" src="/Plug-in/ext/Ext.grid.CheckColumn.js"></script>
<script type="text/javascript">
Ext.onReady(function(){
	var myMask = new Ext.LoadMask(Ext.getBody(), {
                        msg: '请稍候...'
                    });
	var fm = Ext.form;
	var sm = new Ext.grid.CheckboxSelectionModel();
	var fields = [
		sm,
	   {name: 'id', mapping : 'id'},
	   {name: 'field_name', mapping : 'field_name'},
	   {name: 'name', mapping : 'name'},
	   {name: 'type', mapping : 'type'},
	   {name : 'needconfig', mapping : 'needconfig'},
	   {name : 'donotrender', mapping : 'donotrender'}
	];
	var store =  new Ext.data.JsonStore({
    	 id:'store',
 		 url: 'form_config.php',
		 fields: fields
  	});
	var comboStore =  new Ext.data.JsonStore({
    	 id:'comboStore',
 		 url: 'form_config.php',
		 fields: ['name','value']
  	});
  	comboStore.load({params:{op: 'combo_store'}});
	var donotrender = new Ext.grid.CheckColumn({
	    header: "屏蔽渲染",
	    dataIndex: 'donotrender',
	    width: 80
	  });
    var row_id=0;

	var cm = new Ext.grid.ColumnModel([
	{
           id : 'id',
           header: "唯一编号",
           dataIndex: 'id',
           width: 80
        },{
           id : 'field_name',
           header: "字段名",
           dataIndex: 'field_name',
           width: 80
        },{
           id : 'name',
           header: "表单名",
           dataIndex: 'name',
           width: 100,
           editor: new fm.TextField({
               allowBlank: false
           })
        },
        {
           id : 'type',
           header: "类型",
           dataIndex: 'type',
           width: 130,
           editor: new fm.ComboBox({
		            id : 'catalog',
					name : 'catalog',
					emptyText  :'选择一个类型',
					fieldLabel : '类型',
					allowBlank : true,
					store:comboStore,
					hiddenName : 'type',
					selectOnFocus:true,
			        mode:'local',
			        readOnly :true,
			        displayField: "value",
			        valueField :'name',
			        forceSelection: true,
					triggerAction: "all"
			})
        },{id : 'needconfig', header: "配置完成", sortable: true, dataIndex: 'needconfig',renderer:function(val){
				return val==1?'<font color=green><center><b>√</b></center></font>':'<font color=red><center><b>×</b></center></font>';
		}},donotrender
    ]);


	var grid = new Ext.grid.EditorGridPanel({
		id				 :'grid',
        store            : store,
       	cm				 : cm,
       	sm				 : sm,
        autoExpandColumn : 'name',
        plugins			 : [donotrender],
		region           : 'center',
		frame			 : false,
		clicksToEdit	 : 2,
		ddGroup			 :'GridDD',
		enableDragDrop 	 : true,
		border			 : false,
        title            : false,
        tbar:['-',
            <{section name=li loop=$type_array}>
				{xtype: 'button',text:'<{$type_array[li].value}>',handler:function(){top.xajax_items_config('<{$id}>','<{$type_array[li].name}>',true);}
				}
            <{if $smarty.section.li.index+1 < $type_array|@count}>,'-',<{/if}><{/section}>
			,'-'
            ],
        listeners: {
        	'render': function(){
                    bbar2.render(this.tbar);
                },
        	"beforeedit":{
            	fn: function(items){
            	row_id=items.row;
            	},
                scope: this
            },
            "afterEdit": {
                fn: function(items){
                	myMask.show();
                	Ext.Ajax.request(
			                {
			                    waitMsg:'正在提交....',
			                    method:'POST',
			                    params:{op:'items_config_save_ingrid',id:items.record.data.id,value:items.value,field:items.field},
			                    url:'form_config.php',
			                    success:function(form, action) {
			                    	store.reload();
			                   		myMask.hide();
			                    },
			                    failure: function(form, action) {
			                    }
			                }
			            );
            	},
                scope: this
            }
        }
    });
    bbar2 = new Ext.Toolbar({
            renderTo:grid.tbar
            ,items:['->',{
                 text:'刷新',handler:function(){
                    myMask.show();
                 	store.reload();
                 	myMask.hide();
                 }
            },'-',{
                 text:'编辑',handler:function(){
					if(grid.selModel.getCount()==1){
						var selections = grid.selModel.getSelections();
						var param =selections[0].data;
						top.xajax_items_config('<{$id}>',param.field_name);
					}else if(grid.selModel.getCount()>1){
						Ext.MessageBox.alert("提示","本操作无法批量完成");
					}else{
				 		Ext.Msg.alert("提示消息","请选择一行数据然后才能编辑!");return false;
					}

                 }
            },'-',{
                 text:'删除',
                 handler:function(){

                 	if (grid.selModel.getCount()!=1)
                        {
                            Ext.Msg.alert('警告', '请选择一个');
                            return false;
                        }
                    myMask.show();
                   	var selections = grid.selModel.getSelections();
					var param =selections[0].data;
					Ext.Ajax.request(
			                {
			                    waitMsg:'正在提交....',
			                    method:'POST',
			                    params:{op:'form_component_del',id:param.id},
			                    url:'form_config.php',
			                    success:function(form, action) {
			                    	store.reload();
			                   		myMask.hide();
			                    },
			                    failure: function(form, action) {
			                    }
			                }
			            );
                 }
            }, '-'
            ]
        });
    var viewport = new Ext.Viewport({
    	layout : 'fit',
    	items:[
    		grid
    	]
    });
    donotrender.on('click',function(a,b,c){
    	myMask.show();
                	Ext.Ajax.request(
			                {
			                    waitMsg:'正在提交....',
			                    method:'POST',
			                    params:{op:'items_config_save_ingrid',id:c.data.id,value:c.data[a.dataIndex],field:a.dataIndex},
			                    url:'form_config.php',
			                    success:function(form, action) {
			                    	store.reload();
			                   		myMask.hide();
			                    },
			                    failure: function(form, action) {
			                    }
			                }
			            );
    });
	store.load({params:{op: 'formitem',id:'<{$id}>'}});
	 var ddrow = new Ext.dd.DropTarget(grid.getView().mainBody, {
        ddGroup : 'GridDD',
        copy:false,
        notifyDrop : function(dd, e, data) {
            var sm = grid.getSelectionModel();
            var rows = sm.getSelections();

            var cindex = dd.getDragData(e).rowIndex;
            if (cindex == undefined || cindex < 0){
                e.cancel=true;
                return;
            }
            for (i = 0; i < rows.length; i++) {
               rowData = store.getById(rows[i].id);
                if (!this.copy) {
                    store.remove(store.getById(rows[i].id));
                    store.insert(cindex, rowData);
                    return;
                }
            }
            storeData(store);
        }
    });
});
</script>
</head>
<body>

</body>
</html>